//index.js
//获取应用实例
const app = getApp()
const Api = require("../../utils/api.js").API
 const Store = require("../../utils/store.js").store
const codeZ = require("../../utils/util.js")
Page({
    data: {
        hei2:500,
        active:0,
        userStatus:"",
        page:1,
        tabsArray:[{
                "name": "全 部",
                "name1": "全 部",
                'icon_no':'../../images/d12.png',
                'icon_se':'../../images/order11.png',
                "list": [],
                'active':0,
                "total": 0
            },
            {
               "name": "待付款",
                "name1": "客户付款",
              'icon_no':'../../images/order2.png',
              'icon_se':'../../images/oder22.png',
               "list": [],
                'active':1,
               "total": 0
            },
            {
              "name": "待采购",
               "name1": "发起采购",
             'icon_no':'../../images/order3.png',
             'icon_se':'../../images/order33.png',
              "list": [],
               'active':2,
              "total": 0
            },
            {
                "name": "待收货",
                 "name1": "客户收货",
             'icon_no':'../../images/order4.png',
             'icon_se':'../../images/order44.png',
              "list": [],
               'active':3,
              "total": 0
            },
            {
                 "name": "完 成",
                  "name1": "完 成",
            'icon_no':'../../images/order5.png',
            'icon_se':'../../images/order55.png',
             "list": [],
              'active':4,
             "total": 0
            }
        ],
               
    },
   onLoad(options) {
      let that =this;
     
       if (options.hasOwnProperty('type')) {
           this.setData({ 
               active: parseInt(options.type)
           })
       } else {
           this.setData({
              
               active: 0,
   
           })
        }
           
           
         var arr = this.data.tabsArray;
            
            
         arr.forEach((item, index) => {
             item.list = []
         })
         var a = ''+this.data.active;
         let paySta = "";
         let orderSta ="";
            
         if (a == "1") {
             paySta = "110901";
             orderSta = '140302'
         } else if (a == "2") {
            paySta = "110902";
            orderSta = '140302'
         } else if (a == "3") {
            
             paySta = "110903";
             orderSta = '140304'
         }else if (a == "4") {
            paySta = "110903";
            orderSta = '140305'
         }
               
            
          
         var b = Store.getStore('name');
         if (b == "") {
             this.setData({
                 isShow: true
             })
         }
          this.requstList(a,orderSta,paySta,1);
        
          
   //         var pages = getCurrentPages() //  获取页面栈
   //         var prevPage = pages[pages.length - 2] // 上一个页面  
   //         prevPage.setData({
   //             // 给上一个页面变量赋值   
   //             isRouteMy: '2',
   //         })
   //     }
       
   },

   onShow() {
       let baseinfo =  wx.getStorageSync('baseInfo');
          if(baseinfo.hasOwnProperty('ckType')){
              this.setData({
                  userStatus:baseinfo.ckType
              })
          }else{
              this.setData({
                  userStatus:''
              })
          }
     
   this.setData({
       
        hei2:app.globalData.screenHeight-280,
        
   })
   
      
      
   },
   SureShouHuo(e){
       // console.log()
        let data = {'ckOrderNo':e.currentTarget.dataset.item}
        Api.confirm_receiptApi(data).then(res=>{
           
            this.requstList('3','140304','110903',1)
        })
   },
   goLogin: function() {
       wx.navigateTo({
           url: '../login/index',
       })
   },
   requstList: function(status, orderState,payState, page) {
       let that = this;
       var i = 0;
       if (status != "0") {
           i = parseInt(status);
       }
        let list = `tabsArray[${i}].list`;
         let total = `tabsArray[${i}].total`;
       if(page == 1){
           this.setData({
               [list]:[],
               [total]:0
           })
       }
     
   
      var data = {
          "asc": true,
          "isChose": true,
          "limit": 10,
          "orderState": orderState,
          "payState":payState,
          "page": page,
          "sidx": ""
      }
   
      
      
   
       Api.orderListApi(data).then(res => {
           console.log(res);
           if (res.code == 0) {
               var arr = res.data.records;
   
               if (arr.length == 0) {
                   return;
               }
               
               
               var oldArr = that.data.tabsArray[i].list;
   
               var newArr = oldArr.concat(arr);
   
               that.setData({
                   [list]: newArr,
                   [total]: res.data.total,
                   isShow: false,
               })
           } else if (res.code == "401") {
               that.setData({
                   isShow: true,
               })
   
           } else {
   
           }
   
       })
   },
   
   clickDe: function(e) {
   
       let i = e.currentTarget.dataset.inde;
       let j = this.data.active;
       var comSta = `tabsArray[${j}].list[${i}].isSelect`;
       let a = this.data.tabsArray[j].list[i].isSelect;
       if (a == true) {
           this.setData({
               [comSta]: false
           })
       } else {
           this.setData({
               [comSta]: true
           })
       }
       },
   
   //事件处理函数
   cheakAllOrder(e){
           let a = e.currentTarget.dataset.id;
           if(a ==2){
               return wx.navigateTo({
                   url:'../MyCaiGou/index'
               })
           }
           this.setData({
               active:a,
           })
        
      
       let list = `tabsArray[${a}].list`;
       this.setData({
           [list]: [],
            page: 1
   
       })
       let paySta = "";
       let orderSta ="";
   
       if (a == "1") {
           paySta = "110901";
           orderSta = '140302'
       } else if (a == "2") {
          paySta = "110902";
          orderSta = '140302'
       } else if (a == "3") {
          
           paySta = "110903";
           orderSta = '140304'
       }else if (a == "4") {
          paySta = "110903";
          orderSta = '140305'
       }
       this.requstList(a,orderSta,paySta,1)
   
   },
   
   refreshBottom: function(e) {
       var i = e.currentTarget.dataset.index;
     
       var a = "" + e.currentTarget.dataset.index;
       
   
       let paySta = "";
       let orderSta ="";
          
       if (a == "1") {
           paySta = "110901";
           orderSta = '140302'
       } else if (a == "2") {
          paySta = "110902";
          orderSta = '140302'
       } else if (a == "3") {
          
           paySta = "110903";
           orderSta = '140304'
       }else if (a == "4") {
          paySta = "110903";
          orderSta = '140305'
       }
   
       var total = this.data.tabsArray[i].total;
       var list = this.data.tabsArray[i].list;
   
       if (list.length < total) {
           var c = this.data.page;
           c++;
           this.setData({
               page: c
           })
           this.requstList(a,orderSta,paySta,c)
   
       } else {
           this.setData({
               isShowText: i
           })
       }
   
   
   },
   
   SureShouHuo(e){
       
       // let data = {'ckOrderNo':}
       // Api.confirm_receiptApi(data).then(res=>{
           
       // })
   },
   Sure(){
       let that =this;
       let arr = this.data.tabsArray[1].list;
       let NewArray = [];
       arr.forEach(item=>{
           if(item.isSelect){
               NewArray.push(item.ckOrderNo);
           }
       })
       if(NewArray.length == 0){
           wx.showLoading({
               title:'请至少选择一笔订单进行确认!',
               icon:'none'
           })
       }else{
           
            Api.confirm_collectionApi(NewArray).then(res=>{
                
                
                 that.requstList(1,'140302','110901',1)
            })
       }
      
       
   },
  
   Cancel(){
         let that =this;
         let arr = this.data.tabsArray[1].list;
         let NewArray = [];
           arr.forEach(item=>{
               if(item.isSelect){
                   NewArray.push(item.ckOrderNo);
               }
           })
           if(NewArray.length == 0){
               wx.showToast({
                   title:'请至少选择一笔订单进行取消!',
                   icon:'none'
               })
           }else{
               
                Api.confirm_cancelApi(NewArray).then(res=>{
                    
                    
                     that.requstList(1,'140302','110901',1)
                })
           }                  
   },
   backHome(){
       wx.switchTab({
           url:'../index/index'
       })
   },
   backMy(){
       wx.navigateBack();
   },
   
   
  
  

  







});
